<?php
class base
{
	static $db		= array();
	static $table	= array();
	
	static function init($name)
	{
		if (!isset(self::$table[$name]))
		{
			$db	= self::db();
			require_once ROOT . "app/model/$name.lib.php";
			Zend_Db_Table::setDefaultAdapter($db);
			self::$table[$name]	= new $name;
		}
		return self::$table[$name];
	}
	
	protected static function db($config=null)
	{
		if (empty($config))
		{
			require_once ROOT. 'libs/Zend/Registry.php';
			$config = Zend_Registry::get('config');
		}	

		$dbId = md5(join('|',$config));

		if (empty(self::$db[$dbId]))
		{
			require_once ROOT. 'libs/Zend/Db.php';
            self::$db[$dbId] = Zend_Db::factory('mysqli',$config);
            empty($config['charset']) or self::$db[$dbId]->query("SET NAMES '{$config['charset']}'");
		}
		return self::$db[$dbId];
	}
	
	public function dbVersion()
	{
		 return base::db()->fetchOne("SELECT VERSION() AS v");
	}

	public function dbTableStatus()
	{
		return base::db()->fetchAll("SHOW TABLE STATUS");
	}
}
/*
class base 
{
	public $_setVariable;
	public static $_obj = array();
	public $sqlData = array();

	public static function init($n)
	{
		if (empty(self::$_obj[$n]))
		{
			require_once ROOT . "app/model/$n.lib.php";
			self::$_obj[$n]	= new $n;
		}
		$obj	= self::$_obj[$n];
		if (method_exists($obj,"_construct")) $obj->__construct();
		return $obj;
	}
	public function __set($k,$v) 
	{
		$this->_setVariable[$k] = $v;
	}
	
	public function __get($k) 
	{
		return $this->_setVariable[$k];
	}

	public function insert($arg)
	{
		$db	= self::db();
        $db->insert($this->from,array_get($arg,$this->fields));
        return $db->lastInsertId();
	}

	public function where($where)
	{
		if (is_array($where))
		{
            $query	= array();
            $from       = isset($where['from'])     ? $where['from'] : $this->from;
            $fields     = isset($where['fields'])   ? (is_array($where['fields']) ? join(',',$where['fields']) : $where['fields']) : '*';
            $limit      = isset($where['limit'])    ? explode(',',$where['limit']) : '';
            $limit      = !empty($limit)            ? " limit " . ($limit['0']-1)*$limit['1'] . " , " . $limit['1'] : '';
            $group      = isset($where['group'])    ? " group by " . $where['group'] : '';
            $order      = isset($where['order'])    ? " order by " . $where['order'] : '';

            if ( isset($where['from']))    unset($where['from']);
            if ( isset($where['fields']))  unset($where['fields']);
            if ( isset($where['limit']))   unset($where['limit']);
            if ( isset($where['group']))   unset($where['group']);
            if ( isset($where['order']))   unset($where['order']);
            
            foreach ($where as $key => $val)
			{
                if (!is_string($key))
                {
                    $query[] = $val;
                }
                else 
                {
				    $query[] = is_array($val) ? "$key in (" . join(',', $val ) . ")" : "$key='$val'";
                }
			}
			$query = join(" and ",$query);
            !empty($query) || $query = 1;
            $query = "SELECT $fields FROM $from WHERE $query $order $limit $group ";
		}
		else if(!empty($where))
		{
			$query = $where;
		}
		else 
		{
			$query = "SELECT * FROM " . $this->from . " WHERE 1";//var_dump($query);exit;
		}
		return $query;
	}

	public function getRow($where)
	{
		$where		= $this->where($where);
		$md5		= md5($where);
		if (!isset($this->sqlData[$md5]))
		{
			$db		= self::db();
			$this->sqlData[$md5] = $db->fetchRow($where);
		}
		return $this->sqlData[$md5];
	}

	public function getNum($where=null)
	{
		$where['fields'] = ' COUNT(*) ';
		if (isset($where['limit'])){ unset($where['limit']);}
		$where		= $this->where($where);
		$md5		= md5($where);
		if (!isset($this->sqlData[$md5]))
		{
			$db		= self::db();
	        
			$this->sqlData[$md5] = $db->fetchOne($where);
		}
		return $this->sqlData[$md5];
	}

	public function delete($where)
	{
		$db		= base::db();
		$query	= array();

		foreach ($where as $key => $val)
		{
			$query[] = is_array($val) ? "$key in (" . join(',',$val) . ")" : "$key=$val";
		}
		$query = join(" and ",$query);

		return $db->delete($this->from, $query);
	}

	public function edit($where, $edit)
	{
		$db		= base::db();
		$query	= array();

		foreach ($where as $key => $val)
		{
			$query[] = is_array($val) ? "$key in (" . join(',',$val) . ")" : "$key=$val";
		}
		$query = join(" and ",$query);

		return $db->update($this->from, array_get($edit,$this->fields), $query);
	}

	public function getAll($where=null)
	{
		$where		= $this->where($where);
		$md5		= md5($where);
		if (!isset($this->sqlData[$md5]))
		{
			$db	= self::db();
			try {
				$this->sqlData[$md5]	= $db->fetchAll($where);
			}
			catch (Exception $e)
			{
				throw new ErrorException($this->where($where));
			}
		}
		return $this->sqlData[$md5];
	}
	
	public static function fetchall($where)
	{
		return self::db()->fetchAll($where);
	}

	protected static function db($config=null)
	{
		if (empty($config))
		{
			require_once ROOT. 'libs/Zend/Registry.php';
			$config = Zend_Registry::get('config');
		}	

		$dbId = md5(join('|',$config));

		if (empty(self::$_obj['db'][$dbId]))
		{
			require_once ROOT. 'libs/Zend/Db.php';
            self::$_obj['db'][$dbId] = Zend_Db::factory('mysqli',$config);
            empty($config['charset']) or self::$_obj['db'][$dbId]->query("SET NAMES '{$config['charset']}'");
		}
		return self::$_obj['db'][$dbId];
	}
	
	public static function closeDb()
	{
		$db = self::db();
		$conn = $db->getConnection();
		$conn->close();
		self::$_obj['db'] = Null;
		unset(self::$_obj['db']);
	}


	public function dbVersion()
	{
		 return base::db()->fetchOne("SELECT VERSION() AS v");
	}

	public function dbTableStatus()
	{
		return base::db()->fetchAll("SHOW TABLE STATUS");
	}
}
 * 
 */